# @LICENSE(NICTA_CORE, 2011)

# Toolchain settings.
export CC      = gcc
export CFLAGS  = -g
export DEFINES = -DVERSION=\"1.0\"
export INCLUDE = -Isrc/include

# Verbosity. Set V=1 on the command line to echo commands as they are run.
ifeq ($V, 1)
Q =
else
# V > 1 has no effect in this Makefile, but we may be passed this from a parent
# Makefile.
ifeq ($V, 2)
Q =
else
ifeq ($V, 3)
Q =
else
Q = @
endif
endif
endif

# Build paths.
BUILD_ROOT   ?= build
STAGING_ROOT ?= staging
PREFIX       ?= /usr/local

# C files in src/.
SOURCES = aistructs binfo ditekcp dit edit elf32 elf64 elf_generic endian log raw

.PHONY: default clean install uninstall

default: ${BUILD_ROOT}/dite

${BUILD_ROOT}/dite: $(patsubst %,${STAGING_ROOT}/%.o,${SOURCES}) \
                   |${BUILD_ROOT}
	@echo " [LD]    `basename $@`"
	${Q}${CC} -o $@ $^

${STAGING_ROOT}/%.o: src/%.c \
                    |${STAGING_ROOT}
	@echo " [CC]    `basename $@`"
	${Q}${CC} ${CFLAGS} ${DEFINES} ${INCLUDE} -c -o $@ $^

${BUILD_ROOT}:
	@echo " [MKDIR] `basename $@`"
	${Q}mkdir -p ${BUILD_ROOT}

${STAGING_ROOT}:
	@echo " [MKDIR] `basename $@`"
	${Q}mkdir -p ${STAGING_ROOT}

clean:
	rm -rf build
	rm -rf staging

# You may need to be root/sudo for the targets below, depending on PREFIX.
install: ${BUILD_ROOT}/dite
	install ${BUILD_ROOT}/dite ${PREFIX}/bin/dite
	mkdir -p ${PREFIX}/man/man1
	install doc/dite.1 ${PREFIX}/man/man1/dite.1

uninstall:
	rm ${PREFIX}/bin/dite
	rm ${PREFIX}/man/man1/dite.1
